/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package exercicio.lista3;

import java.util.Random;

/**
 *
 * @author afonso
 */
public class Questao3 {

    public static void main(String[] args) {
        letraD();
        letraF();
    }

    public static void letraD() {
        int numExp = 10000;
        double[] val = new double[numExp];

        Random r = new Random();

        for (int i = 0; i < numExp; i++) {
            val[i] = -Math.log(r.nextDouble());
        }

        double media = media(val);
        double variancia = variancia(val);

        System.out.println("d) Média = " + media + "; Variância = " + variancia);
    }

    public static void letraF() {
        int numExp = 10000;
        int cont = 0;

        Random r = new Random();

        for (int i = 0; i < numExp; i++) {
            if (-Math.log(r.nextDouble()) <= 2) {
                cont++;
            }
        }

        double p = (double) cont / numExp;

        System.out.println("f) Probabilidade = " + p);
    }

    public static double media(double[] array) {
        double soma = 0;
        int n = array.length;
        for (int i = 0; i < n; i++) {
            soma += array[i];
        }
        return (double) soma / n;
    }

    public static double variancia(double[] array) {
        double var = 0;
        double media = media(array);
        int tam = array.length;
        for (int i = 0; i < tam; i++) {
            var += Math.pow(array[i] - media, 2) / tam;
        }
        return var;
    }
}
